#include "Roads.h"
#include "D3DApp.h"
#include "RandomNumberGenerator.h"

void Roads::Generate(Terrain* t,std::vector<GameObject*>* g)
{
	m_pvGameobjects = g;
	m_Wasp = new Wasp(t->GetFractal(),t->GetCellSize());
	m_Wasp->GenerateWeights();
}
void Roads::Generate(VTesselation* VT)
{
	HRESULT hr;
	LPD3DXMESH mesh;
	const int height=32;
	const int width=32;
	m_RoadTex=new Texture;

	V( D3DXCreateTexture(TheD3DApp::Instance()->GetD3DDev(),width,height,D3DX_DEFAULT,D3DUSAGE_DYNAMIC,
		D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&m_RoadTex->m_pTex));

	D3DCOLORVALUE color[height*width];
	for (int i=0;i<width*height;++i)
	{
		int x=i%width;
		int y=(i-x)/width;
		if(x<4 || x>width-5)
		{
			color[i].r=0.8;
			color[i].g=0.8;
			color[i].b=0.8;
			color[i].a=1;
		}
		else if(x==width/2 ||x==(width/2)-1)
		{
			color[i].r=1;
			color[i].g=1;
			color[i].b=1;
			color[i].a=1;
		}
		else
		{
			color[i].r=0.4;
			color[i].g=0.4;
			color[i].b=0.4;
			color[i].a=1;
		}
	}
	D3DXFillTexture(m_RoadTex->m_pTex,CF,color);


	int NumVerts=4;
	int NumFaces=2;
	VecLineSeg vls = VT->GetEdges();

	for (unsigned int i=0;i<vls.size();++i)
	{
		D3DXVECTOR2 vec = vls[i].second - vls[i].first;
		D3DXVECTOR3 right(vec.y,0,-vec.x);
		D3DXVec3Normalize(&right,&right);
		//right*=0.1;
		V(D3DXCreateMesh(NumFaces,NumVerts,D3DXMESH_MANAGED,TheD3DApp::Instance()->GetVertexElements(),TheD3DApp::Instance()->GetD3DDev(),&mesh));

		int vertCount=0;
		POSNORMUVVERTEX* pVertices;
		V(mesh->LockVertexBuffer(0,(void**)&pVertices));

		
		pVertices[vertCount].Pos=D3DXVECTOR3(vls[i].first.x,0,vls[i].first.y)+right;
		pVertices[vertCount].Pos.y=0.01;
		pVertices[vertCount].Norm=D3DXVECTOR3(0,1,0);
		pVertices[vertCount].UV=D3DXVECTOR2(0,0);
		vertCount++;

		pVertices[vertCount].Pos=D3DXVECTOR3(vls[i].first.x,0,vls[i].first.y)-right;
		pVertices[vertCount].Pos.y=0.01;
		pVertices[vertCount].Norm=D3DXVECTOR3(0,1,0);
		pVertices[vertCount].UV=D3DXVECTOR2(1,0);
		vertCount++;

		pVertices[vertCount].Pos=D3DXVECTOR3(vls[i].second.x,0,vls[i].second.y)+right;
		pVertices[vertCount].Pos.y=0.01;
		pVertices[vertCount].Norm=D3DXVECTOR3(0,1,0);
		pVertices[vertCount].UV=D3DXVECTOR2(0,1);
		vertCount++;

		pVertices[vertCount].Pos=D3DXVECTOR3(vls[i].second.x,0,vls[i].second.y)-right;
		pVertices[vertCount].Pos.y=0.01;
		pVertices[vertCount].Norm=D3DXVECTOR3(0,1,0);
		pVertices[vertCount].UV=D3DXVECTOR2(1,1);
		vertCount++;

		V(mesh->UnlockVertexBuffer());


		int indCount=0;
		WORD *IndexData=NULL;
		V(mesh->LockIndexBuffer(0,(void**)&IndexData));

		for (unsigned int i=0;i<NumFaces;i+=2)
		{
			IndexData[indCount++]=0;
			IndexData[indCount++]=1;
			IndexData[indCount++]=2;

			IndexData[indCount++]=2;
			IndexData[indCount++]=1;
			IndexData[indCount++]=3;
		}

		V(mesh->UnlockIndexBuffer());
		V(mesh->OptimizeInplace(D3DXMESHOPT_COMPACT ,0,0,0,0));
		V(D3DXComputeNormals(mesh,0));
	
		
		GameObject* g = new GameObject(mesh);
		g->AddTexture(m_RoadTex);
		MainState::Instance()->addGameObject(g);
	}
	

	



}
void Roads::CreateRoad(D3DXVECTOR2 start, D3DXVECTOR2 end)
{
	HRESULT hr;
	LPD3DXMESH mesh;
	std::vector<D3DXVECTOR2> v= m_Wasp->FindPath(start,end);
	//create mesh for the road
	V(D3DXCreateMesh(v.size()-2,v.size(),D3DXMESH_MANAGED,TheD3DApp::Instance()->GetVertexElements(),TheD3DApp::Instance()->GetD3DDev(),&mesh));


	GameObject* res = new GameObject(mesh);
	m_pvGameobjects->push_back(res);
}